package com.haoan.index.service.impl;


import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import com.haoan.index.domain.Place;
import com.haoan.index.mapper.PlaceMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.TicketMapper;
import com.haoan.index.domain.Ticket;
import com.haoan.index.service.TicketService;

/**
 * 门票Service业务层处理
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper,Ticket> implements TicketService
{

    @Resource
    private PlaceMapper placeMapper;
    /**
     * 查询门票
     *
     * @param id 门票主键
     * @return 门票
     */
    @Override
    public Ticket selectTicketById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询门票列表
     *
     * @param ticket 门票
     * @return 门票
     */
    @Override
    public List<Ticket> selectTicketList(Ticket ticket)
    {
        LambdaQueryWrapper<Ticket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ticket.getId() != null, Ticket::getId, ticket.getId());
        if (null != ticket.getRegion()){
            LambdaQueryWrapper<Place> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(Place::getRegion,ticket.getRegion());
            List<Place> places = placeMapper.selectList(wrapper);
            List<Long> ids = new ArrayList<>();
            for (Place place : places) {
                ids.add(place.getId());
            }
            queryWrapper.in( Ticket::getPlaceId, ids);
        }

        queryWrapper.like(ticket.getName() != null, Ticket::getName, ticket.getName());
        queryWrapper.eq(ticket.getPicture() != null, Ticket::getPicture, ticket.getPicture());
        queryWrapper.eq(ticket.getNumber() != null, Ticket::getNumber, ticket.getNumber());
        queryWrapper.eq(ticket.getPrice() != null, Ticket::getPrice, ticket.getPrice());
        queryWrapper.eq(ticket.getManner() != null, Ticket::getManner, ticket.getManner());
        queryWrapper.like(ticket.getSponsor() != null, Ticket::getSponsor, ticket.getSponsor());
        queryWrapper.eq(ticket.getDetail() != null, Ticket::getDetail, ticket.getDetail());
        queryWrapper.eq(ticket.getStatus() != null, Ticket::getStatus, ticket.getStatus());
        queryWrapper.eq(ticket.getCreateTime() != null, Ticket::getCreateTime, ticket.getCreateTime());
        queryWrapper.eq(ticket.getCreateBy() != null, Ticket::getCreateBy, ticket.getCreateBy());
        queryWrapper.eq(ticket.getUpdateTime() != null, Ticket::getUpdateTime, ticket.getUpdateTime());
        queryWrapper.eq(ticket.getUpdateBy() != null, Ticket::getUpdateBy, ticket.getUpdateBy());
        queryWrapper.eq(Ticket::getValiFlag,ValiFlag.VALID );
        List<Ticket> list = baseMapper.selectList(queryWrapper);
        list.forEach(e ->{
            Place place = placeMapper.selectById(e.getPlaceId());
            e.setPlaceName(place.getName());
            e.setRegion(place.getRegion());
        });
        return list;
    }

    /**
     * 新增门票
     *
     * @param ticket 门票
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTicket(Ticket ticket)
    {
        return baseMapper.insert(ticket);
    }

    /**
     * 修改门票
     *
     * @param ticket 门票
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTicket(Ticket ticket)
    {
        return baseMapper.updateById(ticket);
    }

    /**
     * 批量删除门票
     *
     * @param ids 需要删除的门票主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTicketByIds(Long [] ids)
    {
        Ticket ticket = new Ticket();
        ticket.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<Ticket> updQw = new LambdaQueryWrapper<>();
        updQw.in(Ticket::getId, ids);
        return baseMapper.update(ticket, updQw);
    }

    /**
     * 删除门票信息
     *
     * @param id 门票主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        Ticket ticket = getById(id);
        ticket.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(ticket);
    }
}
